package com.frontalcode
{
	import flash.events.*;
	
	/**                  
     * The OptionableObject class supports options with defaults.
     */
	public class OptionableObject extends ProfiledObject
	{
		/* 
			Variable: objectOptions
			
				An object containing the options for this object.
		*/
		/**
		 * @private
		 */
		internal var objectOptions : Object = { };
		
		/*
			Variable: objectOptionDefaults
			
				An object containing the default values for the options of this object.
		*/
		/**
		 * @private
		 */
		internal var objectOptionDefaults : Object = { };
		
		/**
		 * Creates a new OptionableObject instance.
		 *
		 * @param options	An Object containing the desired options for this instance.
		*/	
		public function OptionableObject ( options : Object = null )
		{
			super ( );
			
			setObjectOptions ( options );
		}
		
		/*
			Function: setObjectOptionDefault
		
				Set the default value for an option. When getting the value of an 
				option, we first look at the option value and then these defaults.
			
			Parameters:
			
				option - the option name
				value  - the option value
				
			See Also:
			
				<setObjectOption>
		*/		
		public function setObjectOptionDefault ( option : String, value : * ) : *
		{
			return objectOptionDefaults [ option ] = value;
		}
		
		/*
		
			Function: setObjectOption
		
				Set options that change the basic behavior of the object.
		
			Parameters:
			
				option 	- the option name
				value 	- the option value
			
			See Also:
			
				<setObjectOptionDefault>
		*/
		public function setObjectOption ( option : String, value : * ) : *
		{
			if ( value === undefined )
			{
				delete objectOptions [ option ];
			}
			else
			{
				objectOptions [ option ] = value;
			}
			
			return value;
		}
		
		/*
			Function: setObjectOptions
		
				Set a bunch of options at once.
			
			Parameters:
			
				options - an object containing multiple options.
		*/
		public function setObjectOptions ( options : Object ) : void
		{
			for ( var option : String in options )
			{
				setObjectOption ( option, options [ option ] );
			}
		}
	
		/*
			Function: getObjectOption
	
				Get the value of an option. Checks option value and if none exists
				returns the default value.
		*/
		public function getObjectOption ( option : String ) : *
		{
			var value : Object;
			
			if ( objectOptions.hasOwnProperty ( option ) )
			{
				value = objectOptions [ option ];
			}
			else
			{
				value = objectOptionDefaults [ option ];
			}
			
			return value;
		}
		
		/*
			Function: setOptionsFromLoaderParameters
	
				Allow any option to be set via FlashVars or equivalent..
		*/
		public function setOptionsFromLoaderParameters ( ) : void
		{
			for ( var option : String in objectOptionDefaults )
			{
				var value : * = DocumentManager.getInstance ( ).getLoaderParameter ( option );
				
				if ( value !== undefined )
				{
					setObjectOption ( option, value );
				}
			}
		}
	}
}

